package com.auerdi.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.auerdi.ui.order.ADOrderFragment;
import com.example.eggframe.WechatEvent;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.PayWeixinResponce;
import com.example.egghttp.retrofit.params.ClientNotifyRequest;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhy.autolayout.AutoLayoutActivity;

import net.sourceforge.simcpux.Constants;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class WXPayEntryActivity extends AutoLayoutActivity implements IWXAPIEventHandler {

    private static final String TAG = "hcia";
    @BindView(R.id.left)
    CardView left;
    @BindView(R.id.right)
    CardView right;
    @BindView(R.id.paysuc)
    ImageView paysuc;
    @BindView(R.id.payerr)
    ImageView payerr;
    @BindView(R.id.suc_txt)
    TextView sucTxt;
    @BindView(R.id.err_txt)
    TextView errTxt;
    @BindView(R.id.container)
    RelativeLayout container;

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        ButterKnife.bind(this);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                EventBus.getDefault().post(new WechatEvent(1));
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

                EventBus.getDefault().post(new WechatEvent(0));

            }
        });
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {


        L.d("hcia", "req:" + req);
        L.d("hcia", "req:" + req.getType());


    }

    @Override
    public void onResp(BaseResp resp) {


        if (resp.errCode != 0) {

            Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

            paysuc.setVisibility(View.GONE);
            payerr.setVisibility(View.VISIBLE);
            sucTxt.setVisibility(View.GONE);
            errTxt.setVisibility(View.VISIBLE);
            left.setVisibility(View.GONE);
            right.setVisibility(View.GONE);
            container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

        } else {

            Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

            paysuc.setVisibility(View.VISIBLE);
            payerr.setVisibility(View.GONE);
            sucTxt.setVisibility(View.VISIBLE);
            errTxt.setVisibility(View.GONE);

            // 付款成功
            RetrofitClient.getPDAClient().create(AdService.class).clientNotify(new ClientNotifyRequest("weixin", ADOrderFragment.payWeixinResponce.getOrder_sn(), "android pay success"))
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BOb<PayWeixinResponce>>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(BOb<PayWeixinResponce> payWeixinResponceBOb) {


                        }
                    });


        }
    }
}